package demo2;


import java.util.Arrays;

class MyArrayList{
    private int[] elem;//存放数据元素
    private int usedSize;//代表当前顺序表当中的有效数据个数  默认为0

    private static final int DEFAULT_SIZE=2;

    public MyArrayList() {
        this.elem = new int[DEFAULT_SIZE];
    }

    public void checkPos(int pos){
        if(pos<0||pos>=usedSize){
            throw new RuntimeException(pos+"位置不合法");
        }
    }

    //遍历顺序表
    public void display(){
        for (int i = 0; i < this.usedSize; i++) {
            System.out.print(this.elem[i]+" ");
        }
        System.out.println();
    }
    //判断是否为空
    public boolean isFull(){
        if(this.usedSize==this.elem.length){
            return true;
        }
        return false;
    }
    //增加数据
    public void add(int data){
        if (isFull()){
            //扩容
            this.elem= Arrays.copyOf(this.elem,2*this.elem.length);
        }
        this.elem[this.usedSize]=data;
        this.usedSize++;
    }
    //指定位置增加数据
    public void add(int pos,int data){
        if(pos<0||pos>usedSize){
            throw new RuntimeException(pos+"位置不合法");
        }
        //1、挪数据，从后往前挪
        if(isFull()){
            this.elem=Arrays.copyOf(this.elem,2*this.elem.length);
        }
        for (int i = this.usedSize-1; i >=pos; i--) {
            elem[i+1]=elem[i];
        }
        //2、放数据
        elem[pos]=data;
        //3、
        usedSize++;
    }
    //判断是否包含某个元素
    public boolean contains(int toFind){
        for (int i = 0; i < usedSize; i++) {
            if(this.elem[i]==toFind){
                return true;
            }
           /* 如果要找的是引用类型 public boolean equals()||public int compareTo()*/
        }
        return false;
    }
    //查找某个元素对应的位置
    public int indexOf(int toFind){
        for (int i = 0; i < usedSize; i++) {
            if(this.elem[i]==toFind){
               return i;
            }
        }
        return -1;
    }
    //获取pos位置的元素
    public int get(int pos){
        checkPos(pos);
        return this.elem[pos];
    }
    //给pos位置的元素设为 value
    public void set(int pos,int value){
        checkPos(pos);
        this.elem[pos]=value;
    }
    //删除第一次出现的关键字key
    public void remove(int toRemove){
        int index=indexOf(toRemove);
        if(index==-1){
            System.out.println("没有这个数据");
            return;
        }
        for (int i = index; i < this.usedSize-1; i++) {
            this.elem[i]=this.elem[i+1];
        }
        //this.elem[usedSize-1]=null
        this.usedSize--;
    }
    //获取顺序表的长度
    public int size(){
        return this.usedSize;
    }
    //清空顺序表
    public void clear(){
        /*//内存泄漏--引用对象
        for (int i = 0; i < this.usedSize; i++) {
            this.elem[i]=null;
        }*/
        this.usedSize=0;
    }


}

public class Test {
    public static void main(String[] args) {
        MyArrayList myArrayList=new MyArrayList();
        System.out.println(myArrayList.isFull());

        myArrayList.add(1);
        myArrayList.add(1,2);
        myArrayList.display();

        System.out.println(myArrayList.contains(1));
        System.out.println(myArrayList.indexOf(1));

        System.out.println(myArrayList.get(1));

        myArrayList.remove(1);
        myArrayList.display();

    }
}
